﻿using LD.ZKT;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace LD
{
    public partial class FrmUser : Form
    {
        private ReDrawUI ReDrawUI = new ReDrawUI();
        private UserHelper user = UserHelper.Instance;
        public FrmUser()
        {
            InitializeComponent();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            var rad = sender as RadioButton;
            if (rad.Checked)
            {
                rad.BackColor = Color.Green;
            }
            else
            {
                rad.BackColor = SystemColors.Window;
            }

            int index = Convert.ToInt16(rad.Tag);
            this.cob_userName.SelectedIndex = index;
        }

        private void FrmUser_Load(object sender, EventArgs e)
        {
            this.Resize += new EventHandler(FrmUser_Resize);
            ReDrawUI.ControlInitializeSize(this);
            LoadUser();

            //ZKTHelper.Instance.Init();
            //ZKTHelper.Instance.Open();

            //ZKTHelper.Instance.MessageChangeEvent += MessageChangeEvent;

            btn_op.Checked = true;
        }

        private void MessageChangeEvent(string message)
        {
            if (!this.IsDisposed)
            {
                this.Invoke(new Action(() =>
                {
                    if (message.Contains("识别成功"))
                    {
                        string id = message.Substring(4, message.Length - 4);
                        var matchUser = DBHelper.Instance.Sql.Queryable<TB_UserTouch>()
                                            .Where(p => p.Id == int.Parse(id)).First();
                        var selectUser = user.GetUsers.Where(p => p.Name == this.cob_userName.Text).FirstOrDefault();
                        if (matchUser != null && matchUser.UserId == selectUser.Id)
                        {
                            user.Current = selectUser;
                            this.lab_loginError.Visible = true;
                            this.lab_loginError.Text = "识别成功";
                            return;
                        }
                        else
                        {
                            this.lab_loginError.Visible = true;
                            this.lab_loginError.Text = "指纹错误";
                            return;
                        }
                    }
                    this.lab_loginError.Text = message;
                    this.lab_loginError.Visible = true;
                }));
            }
        }

        private void LoadUser()
        {
            this.cob_userName.Items.Clear();
            foreach (var item in user.GetUsers ?? new List<UserModel>())
            {
                this.cob_userName.Items.Add(item.Name);
            };
        }
        private void FrmUser_Resize(object sender, EventArgs e)
        {
            ReDrawUI.ControlAutoSize(this);
        }

        private void rab_engineer_Click(object sender, EventArgs e)
        {
            if (UserHelper.Instance.Current.UserType == UserType.管理员 ||
                UserHelper.Instance.Current.UserType == UserType.工程师)
            {
                rab_engineer.Checked = true;
            }
        }

        private void rab_admin_Click(object sender, EventArgs e)
        {
            if (UserHelper.Instance.Current.UserType == UserType.管理员)
            {
                rab_admin.Checked = true;
            }
        }

        private void btn_login_Click(object sender, EventArgs e)
        {
            this.lab_loginError.Visible = false;
            string userName = this.cob_userName.Text;
            string pwd = this.txt_pwd.Text;
            var current = user.GetUsers.Where(p => p.Name.Equals(userName) && p.Pwd.Equals(pwd)).FirstOrDefault();
            if (current != null)
            {
                user.Current = current;
                this.lab_loginError.Visible = true;
                lab_loginError.Text = "登录成功";
            }
            else
            {
                this.lab_loginError.Visible = true;
                lab_loginError.Text = "登录失败";
            }

            this.txt_pwd.Text = string.Empty;

            this.dgv_user.Rows.Add($"【{userName}】" + lab_loginError.Text);
        }

        private void btn_logout_Click(object sender, EventArgs e)
        {
            this.txt_pwd.Text = string.Empty;
            user.Current = new UserModel() { Name = UserType.操作工.ToString() };
        }

        private void btn_touchLogin_Click(object sender, EventArgs e)
        {
            this.lab_loginError.Visible = true;
            this.lab_loginError.Text = "请录入指纹";
            ZKTHelper.Instance.Identify();
        }

        private void lab_pwdChange_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.lab_loginError.Text = "";
            this.lab_loginError.Visible = false;
            FrmPwdChange frmPwd = new FrmPwdChange(this.cob_userName.Text);
            frmPwd.ShowDialog();
            this.lab_loginError.Text = "";
            this.lab_loginError.Visible = true;
        }

        private void lab_register_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.lab_loginError.Text = "";
            this.lab_loginError.Visible = false;

            if (user.Current.UserType != UserType.管理员)
            {
                MessageBox.Show("非管理员权限!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            FrmRegister frmRegister = new FrmRegister();
            frmRegister.ShowDialog();

            this.lab_loginError.Text = "";
            this.lab_loginError.Visible = true;
        }

        private void FrmUser_FormClosing(object sender, FormClosingEventArgs e)
        {
            ZKTHelper.Instance.Close();
            ZKTHelper.Instance.Dispose();
        }

        private void txt_pwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btn_login_Click(sender, e);
            }
        }

        private void cob_userName_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
